package model
{
	//桌子列表
	
	import flash3.framework.ListManager;
	import flash3.framework.Map;
	import flash3.framework.MapManager;
	import flash3.framework.Singleton;
	
	import global.Define;
	
	import vo.RoomUser;
	import vo.TableInfo;
	import vo.UserSit;
	import vo.UserStatus;
	
	public class TableStatusModel extends MapManager
	{
		public function TableStatusModel()
		{
			super();
		}
		
		static public function get i():TableStatusModel
		{
			return Singleton.getInstanceOrCreate(TableStatusModel) as TableStatusModel;
		}
		
		public function getTableInfo(tableID:uint):TableInfo
		{
			var tableInfo:TableInfo = value(tableID) as TableInfo;
			if(!tableInfo)
				add(tableID, new TableInfo);
			return value(tableID);
		}
		public function removeUserByUserID(userID:uint):void
		{
			var tableInfo:TableInfo = null;
			for(var i:uint = 0; i < length; i++)
			{
				tableInfo = value(i);
				if(tableInfo)
				{
					for(var j:uint = 0; j < Define.GAME_PLAYER; j++)
					{
						if(tableInfo.userStatus[j] && tableInfo.userStatus[j].dwUserID == userID)
						{
							tableInfo.userStatus[j] = null;
							return;
						}
					}
				}
			}
		}
		public function addUserByUserStatus(userStatus:UserStatus):void
		{
			var tableInfo:TableInfo = getTableInfo(userStatus.wTableID);
			tableInfo.wSiteID = userStatus.wSiteID;
			tableInfo.wTableID = userStatus.wTableID;
			tableInfo.userStatus[userStatus.wChairID] = userStatus;
		}
		
		public function getRandomChair():UserSit
		{
			var userSit:UserSit = null;
			var tableInfo:TableInfo = null;
			for(var i:uint = 0; i < length; i++)
			{
				tableInfo = value(i);
				for(var j:uint = 0; j < Define.GAME_PLAYER; j++)
				{
					if(!tableInfo.userStatus[j])
					{
						userSit = new UserSit();
						userSit.wSiteID = tableInfo.wSiteID;
						userSit.wTableID = tableInfo.wTableID;
						userSit.wChairID = j;
						userSit.szTablePass = tableInfo.szTablePass;
						return userSit;
					}
				}
			}
			return null;
		}
	}
}